<!--
  speedy-vision.js
  GPU-accelerated Computer Vision for JavaScript
  Copyright 2020-2024 Alexandre Martins <alemartf(at)gmail.com>

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  warping.html
  Image warping demo
-->
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="speedy-vision.js: GPU-accelerated Computer Vision for JavaScript">
        <meta name="author" content="Alexandre Martins">
        <title>Image warping</title>
        <link href="demo-base.css" rel="stylesheet">
        <script src="demo-base.js"></script>
        <script src="../dist/speedy-vision.js"></script>
    </head>
    <body>
        <h1>Image warping</h1>
        <form>
            <div>
                Select the
                <select id="template"></select>
                template, drag the circles or edit the homography matrix below:
            </div>
            <div class="separator"></div>
            <div>
                <table>
                    <tr>
                        <td><input type="number" step="0.1" id="homography-0" value="1"></td>
                        <td><input type="number" step="0.1" id="homography-3" value="0"></td>
                        <td><input type="number" step="0.1" id="homography-6" value="0"></td>
                    </tr>
                    <tr>
                        <td><input type="number" step="0.1" id="homography-1" value="0"></td>
                        <td><input type="number" step="0.1" id="homography-4" value="1"></td>
                        <td><input type="number" step="0.1" id="homography-7" value="0"></td>
                    </tr>
                    <tr>
                        <td><input type="number" step="0.1" id="homography-2" value="0"></td>
                        <td><input type="number" step="0.1" id="homography-5" value="0"></td>
                        <td><input type="number" step="0.1" id="homography-8" value="1"></td>
                    </tr>
                </table>
            </div>
        </form>
        <div>
            <span id="status" style="position:absolute;left:320px;transform:translateX(-50%);"></span>
            <canvas id="canvas-demo"></canvas>
        </div>
        <div>
            <button id="play">Play / pause</button>
        </div>
        <video id="video"
            poster="../assets/loading.jpg"
            loop muted autoplay playsinline hidden
            title="Free video by Cinema Professionals, https://www.pexels.com/pt-br/video/assistindo-a-aguia-voar-3635378/">
                <source src="../assets/flying-eagle.webm" type="video/webm" />
                <source src="../assets/flying-eagle.mp4" type="video/mp4" />
        </video>
        <script>
const VERTEX_RADIUS = 5;
const vertices = [
    { x: 0, y: 0, drag: false },
    { x: 0, y: 0, drag: false },
    { x: 0, y: 0, drag: false },
    { x: 0, y: 0, drag: false }
];
const mouse = { x: 0, y: 0, drag: false };
const template = {
    // matrices are specified in column-major format

    "Identity":
    [
        1, 0, 0, // 1st column
        0, 1, 0, // 2nd column
        0, 0, 1, // 3rd column
    ],

    "Flip":
    [
        -1, 0, 0,
        0, 1, 0,
        639, 0, 1,
    ],

    "Scale":
    [
        0.5, 0, 0,
        0, 0.5, 0,
        0, 0, 1,
    ],

    "Translation":
    [
        0.5, 0, 0,
        0, 0.5, 0,
        200, 100, 1,
    ],

    "Rotation":
    [
        0.86, 0.5, 0,
        -0.5, 0.86, 0,
        400, 50, 2,
    ],

    "Shear":
    [
        0.5, 0, 0,
        -0.5, 0.5, 0,
        250, 100, 1,
    ],

    "Perspective":
    [
        0.2, 0, 0,
        -0.5, -0.15, -0.0015,
        280, 180, 1,
    ],
};

window.onload = async function()
{
    // load a video
    const video = document.getElementById('video');
    const media = await Speedy.load(video);

    // setup the canvas
    const canvas = setupCanvas('canvas-demo', media.width, media.height, video.title);
    addMouseListeners(canvas);

    // create the pipeline
    const pipeline = Speedy.Pipeline();
    const source = Speedy.Image.Source();
    const sink = Speedy.Image.Sink();
    const perspective = Speedy.Transform.PerspectiveWarp();

    source.media = media;

    source.output().connectTo(perspective.input());
    perspective.output().connectTo(sink.input());
    pipeline.init(source, sink, perspective);

    // input fields
    const homographyElement = [...(new Array(9)).keys()].map(k => document.getElementById('homography-' + k));

    // setup the <select> element
    const templateSelector = document.getElementById('template');
    for(let templateName in template) {
        const option = document.createElement('option');
        const text = document.createTextNode(templateName);
        option.value = templateName;
        option.selected = (option.value == 'Identity');
        option.appendChild(text);
        templateSelector.appendChild(option);
    }
    templateSelector.onchange = () => {
        const name = templateSelector.value;
        homographyElement.forEach((inputField, i) => inputField.value = template[name][i]);
        syncVertices();
    };

    // sync the vertices
    async function syncVertices()
    {
        const homography = Speedy.Matrix(3, 3, homographyElement.map(k => +k.value));
        const src = Speedy.Matrix(2, 4, [
            0, 0,
            canvas.width, 0,
            canvas.width, canvas.height,
            0, canvas.height
        ]);

        const dest = Speedy.Matrix.Zeros(2, 4);
        await Speedy.Matrix.applyPerspectiveTransform(dest, src, homography);

        for(let i = 0; i < dest.columns; i++) {
            vertices[i].x = dest.at(0, i);
            vertices[i].y = dest.at(1, i);
        }
    }
    homographyElement.map(el => el.oninput = syncVertices);
    syncVertices(); // initialize vertices

    // update vertices
    async function updateVertices()
    {
        // drag the vertices
        const r = VERTEX_RADIUS;
        const r2 = r * r;
        let anydrag = vertices.reduce((d, v) => d || v.drag, false);
        let anyhover = false;
        for(const vertex of vertices) {
            const dx = mouse.x - vertex.x;
            const dy = mouse.y - vertex.y;
            const nearby = (dx * dx + dy * dy <= r2);

            if(!anydrag && mouse.drag && nearby)
                anydrag = vertex.drag = true;
            anyhover = anyhover || nearby;

            if(vertex.drag) {
                if((vertex.drag = mouse.drag)) {
                    vertex.x = mouse.x;
                    vertex.y = mouse.y;
                }
            }
        }
        canvas.style.cursor = anydrag || anyhover ? 'pointer' : 'auto';

        // if we're dragging the vertices,
        // compute a new homography
        if(anydrag) {
            const src = Speedy.Matrix(2, 4, [
                0, 0,
                canvas.width, 0,
                canvas.width, canvas.height,
                0, canvas.height
            ]);
            const dest = Speedy.Matrix(2, 4,
                vertices.reduce((entries, v) => entries.concat([v.x, v.y]), [])
            );

            const homography = Speedy.Matrix.Zeros(3, 3);
            await Speedy.Matrix.perspective(homography, src, dest);

            const entries = homography.read();
            for(let i = 0; i < entries.length; i++)
                homographyElement[i].value = entries[i];
        }

        // update the pipeline
        const homography = Speedy.Matrix(3, 3, homographyElement.map(k => Number(k.value)));
        perspective.transform = homography;

        // loop
        setTimeout(updateVertices, 1000 / 60);
    }
    updateVertices();

    // Main loop
    (function() {
        let image = null, frameReady = false;

        const context = canvas.getContext('2d');
        context.fillStyle = '#ff0';
        context.strokeStyle = '#f00';
        context.lineWidth = 2;

        async function update()
        {
            const result = await pipeline.run();
            image = result.image;

            frameReady = true;
            setTimeout(update, 1000 / 60);
        }
        update();

        function render()
        {
            if(frameReady) {
                // render the warped image
                renderMedia(image, canvas);

                // render the vertices
                const TWO_PI = Math.PI * 2;
                context.beginPath();
                for(const vertex of vertices) {
                    context.moveTo(vertex.x + VERTEX_RADIUS, vertex.y);
                    context.arc(vertex.x, vertex.y, VERTEX_RADIUS, 0, TWO_PI);
                }
                context.fill();
                context.stroke();
            }

            frameReady = false;
            requestAnimationFrame(render);
        }
        render();

        setInterval(renderStatus, 200);
    })();

    // play/pause
    const playButton = document.getElementById('play');
    playButton.onclick = () => video.paused ? video.play() : video.pause();
}

function addMouseListeners(canvas)
{
    const width = canvas.width;
    const height = canvas.height;

    canvas.addEventListener('mousedown', ev => {
        const position = cursorPosition(canvas, ev);
        mousedrag(position.x, position.y, width, height, true);
    });

    canvas.addEventListener('mouseup', ev => {
        const position = cursorPosition(canvas, ev);
        mousedrag(position.x, position.y, width, height, false);
    });

    canvas.addEventListener('mousemove', ev => {
        const position = cursorPosition(canvas, ev);
        mousedrag(position.x, position.y, width, height);
    });

    return canvas;
}

function cursorPosition(canvas, event)
{
    const rect = canvas.getBoundingClientRect();
    const x = event.clientX - rect.left;
    const y = event.clientY - rect.top;

    return { x, y };
}

function mousedrag(x, y, width, height, drag)
{
    if(drag !== undefined)
        mouse.drag = drag;

    if(x >= 0 && x < width)
        mouse.x = x;

    if(y >= 0 && y < height)
        mouse.y = y;
}
        </script>
        <section id="speedy-vision">
            <span>Powered by <a href="https://github.com/alemart/speedy-vision" target="_blank">speedy-vision.js</a></span>
            <a href="https://github.com/sponsors/alemart" target="_blank" class="button">
                <img alt="GitHub Sponsors" src="https://img.shields.io/github/sponsors/alemart?style=for-the-badge&logo=github&label=Sponsor&labelColor=royalblue&color=gold">
            </a>
        </section>
    </body>
</html>